## Segmented Communities in the Global South: Where Do IR Argentine Scholars Publish and Why? ##
## Montal, Pauselli & Yamin Vázquez ##

data_PS <- read.csv("data_PS",encoding="UTF-8")


#### Statistical Models ####
m1<-glm(scopus~ratio_women+ratio_phdN+ratio_phdS+ratio_conicet+peer.mean.sc+coauthorN+topic_demo+topic_ipe+topic_fpa+topic_seg+topic_theory+latin_america+quant+ref.north.p3, data=data_PS, family = binomial(link="logit"))
m2<-glm(scopusN~ratio_women+ratio_phdN+ratio_phdS+ratio_conicet+peer.mean.an+coauthorN+topic_demo+topic_ipe+topic_fpa+topic_seg+topic_theory+latin_america+quant+ref.north.p3, data=data_PS, family = binomial(link="logit"))

#### Table ####

library(stargazer)

var.labels<-c("Women (ratio)", "PhD Global North (ratio)", "PhD Global South (ratio)", 
              "National Research Council (ratio)", "Peers in Scopus (ratio)", "Peers in Scopus+North (ratio)", "Coauthor North",  "Topic: Democracy and HR",
              "Topic: IPE", "Topic: FPA", "Topic: Security", "Topic: Theory", "Area: Latin America", "Methods: quantitative",
              "References: North")
stargazer(m1,m2, out="modelos.html", no.space=T,title = "Logistic Regressions",
          covariate.labels = var.labels, dep.var.labels = c("Scopus", "Scopus + North"))


#### Plots ####

library(ggpubr)
library(ggplot2)

## Plot Depedent Variable - Figure 1 ##

data_PS<-cbind(data_PS, scopusnoanglo=data_PS$scopus-data_PS$scopusN)
data_PS<-cbind(data_PS, scopus.plot=data_PS$scopus+data_PS$scopusN)

data_PS$scopus.plot<-factor(data_PS$scopus.plot, levels=c("2","1", "0"))

ggplot(data_PS, aes(x=year, fill=scopus.plot))+
  geom_histogram(bins=13, binwidth=0.5 )+
  theme_bw()+
  scale_fill_grey(start=0.75, end=0.25, name="Source", labels=c("Scopus English","Scopus Others",  "Not Scopus"))+
  scale_x_continuous("", breaks=c(2008:2020))+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust=1, size=6), 
        axis.text.y = element_text(size=6), legend.title = element_text(size=6),
        legend.text = element_text(size=6))+
  scale_y_continuous("")



## Plot Prediction - Figure 2##

data.predict<-with(data_PS, data.frame(ratio_women=mean(ratio_women),
                                    ratio_phdN=mean(ratio_phdN),
                                    ratio_phdS=seq(0,1,by=0.01),
                                    ratio_conicet=mean(ratio_conicet),
                                    peer.mean.sc=mean(peer.mean.sc),
                                    peer.mean.an=mean(peer.mean.an),
                                    coauthorN=mean(coauthorN), 
                                    topic_demo=mean(topic_demo),
                                    topic_ipe=mean(topic_ipe),
                                    topic_fpa=mean(topic_fpa),
                                    topic_seg=mean(topic_seg),
                                    topic_theory=mean(topic_theory),
                                    latin_america=mean(latin_america),
                                    quant=mean(quant),
                                    ref.north.p3=mean(ref.north.p3)
                                    
))

pred.prob<-predict(m1, data.predict, type="response", se.fit=T)

data.predict<-cbind(data.predict, fit=pred.prob$fit, se.fit=pred.prob$se.fit)
data.predict<-cbind(data.predict, lower=data.predict$fit-1.96*data.predict$se.fit,
                    upper=data.predict$fit+1.96*data.predict$se.fit)

pred.prob<-predict(m2, data.predict, type="response", se.fit=T)

data.predict<-cbind(data.predict, fit2=pred.prob$fit, se.fit2=pred.prob$se.fit)
data.predict<-cbind(data.predict, lower2=data.predict$fit2-1.96*data.predict$se.fit2,
                    upper2=data.predict$fit2+1.96*data.predict$se.fit2)

data.predict<-cbind(data.predict, place=0)
data.predict.final<-data.predict

data.predict<-with(data_PS, data.frame(ratio_women=mean(ratio_women),
                                       ratio_phdN=seq(0,1,by=0.01),
                                       ratio_phdS=mean(ratio_phdS),
                                       ratio_conicet=mean(ratio_conicet),
                                       peer.mean.sc=mean(peer.mean.sc),
                                       peer.mean.an=mean(peer.mean.an),
                                       coauthorN=mean(coauthorN), 
                                       topic_demo=mean(topic_demo),
                                       topic_ipe=mean(topic_ipe),
                                       topic_fpa=mean(topic_fpa),
                                       topic_seg=mean(topic_seg),
                                       topic_theory=mean(topic_theory),
                                       latin_america=mean(latin_america),
                                       quant=mean(quant),
                                       ref.north.p3=mean(ref.north.p3)
                                       
))

pred.prob<-predict(m1, data.predict, type="response", se.fit=T)

data.predict<-cbind(data.predict, fit=pred.prob$fit, se.fit=pred.prob$se.fit)
data.predict<-cbind(data.predict, lower=data.predict$fit-1.96*data.predict$se.fit,
                    upper=data.predict$fit+1.96*data.predict$se.fit)

pred.prob<-predict(m2, data.predict, type="response", se.fit=T)

data.predict<-cbind(data.predict, fit2=pred.prob$fit, se.fit2=pred.prob$se.fit)
data.predict<-cbind(data.predict, lower2=data.predict$fit2-1.96*data.predict$se.fit2,
                    upper2=data.predict$fit2+1.96*data.predict$se.fit2)

data.predict<-cbind(data.predict, place=1)
data.predict.final<-rbind(data.predict.final, data.predict)
data.predict<-data.predict.final
data.predict<-cbind(data.predict, x.phd=rep(seq(0,1,by=0.01),2))


m1.plot<-ggplot(data.predict, aes(x=x.phd, y=fit, fill=factor(place)))+
  geom_ribbon(aes(ymin=lower, ymax=upper, fill=factor(place)), alpha=0.6)+
  geom_path(aes(color=factor(place)), alpha=0.8)+
  geom_hline(yintercept=0, size=0.2)+ 
  theme_bw()+
  guides(color=FALSE)+
  labs( y="Scopus \n Predicted Probability", x="PhD Ratio")+
  theme(plot.margin = unit(c(0.2,0.8,0.2,0.2), "cm"))+
  scale_fill_grey( start=0.3, end=0.7, name="Education",
                       labels=c("Global South", "Global North"))+
  scale_color_grey(start=0.3, end=0.7)+
  scale_color_grey(start=0.3, end=0.7)+
  theme(axis.text.x = element_text(size=6),
        axis.text.y = element_text(size=6),
        axis.title.x = element_text(size=7),
        axis.title.y = element_text(size=7),
        legend.title = element_text(size=7),
        legend.text = element_text(size=6))

m2.plot<-ggplot(data.predict, aes(x=x.phd, y=fit2, fill=factor(place)))+
  geom_ribbon(aes(ymin=lower2, ymax=upper2, fill=factor(place)), alpha=0.6)+
  geom_path(aes(color= factor(place)), alpha=0.8)+
  geom_hline(yintercept=0, size=0.2)+ 
  ylim(0,0.6)+
  theme_bw()+
  guides(color=FALSE)+
  labs( y="Scopus + North \n Predicted Probability", x="PhD Ratio")+
  theme(plot.margin = unit(c(0.2,0.8,0.2,0.2), "cm"))+
  scale_fill_grey( start=0.3, end=0.7, name="Education",
                       labels=c("Global South", "Global North"))+
  scale_color_grey(start=0.3, end=0.7)+
  theme(axis.text.x = element_text(size=6),
        axis.text.y = element_text(size=6),
        axis.title.x = element_text(size=7),
        axis.title.y = element_text(size=7),
        legend.title = element_text(size=7),
        legend.text = element_text(size=6))


ggarrange(m1.plot, m2.plot, common.legend = TRUE, legend="bottom")

## Plot topics ##

topics<-data_PS[c(13:17)]

topics$topic_demo[topics$topic_demo==1]<-"demo"
topics$topic_ipe[topics$topic_ipe==1]<-"ipe"
topics$topic_fpa[topics$topic_fpa==1]<-"fpa"
topics$topic_seg[topics$topic_seg==1]<-"seg"
topics$topic_theory[topics$topic_theory==1]<-"theory"

topics<-data.frame(topics = c(t(topics)))
topics<-topics[topics$topics!=0,]
topics<-as.data.frame(topics)
topics<-cbind(topics, color=as.numeric(topics$topics))

#Figure 1 in appendix
ggplot(topics, aes(x=topics, fill=topics))+
  geom_bar()+
  theme_bw()+
  scale_x_discrete(name="", labels=c( "Democracy \n& HR", "FPA", "IPE",  "Security", "Theory"))+
  scale_y_continuous("Articles")+  
  theme(legend.position = "none")+
  scale_fill_grey(start=0.3,end=0.7)






